package org.seiya.web.controller.base;


import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 75306469@qq.com on 2016/4/24.
 */

public abstract class Base {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 封装 request.Attribute
     *
     * @param request
     * @param key
     * @return
     */
    public String getAttributeValue(HttpServletRequest request, String key) {
        Object o = request.getAttribute(key);
        if (o != null) {
            return o.toString();
        }
        String oo = request.getParameter(key);
        if (StringUtils.isNotBlank(oo)) {
            return oo;
        }
        return null;
    }

    public String errorParameter() {
        return createError("参数有问题");
    }

    public String createError(String errorInfo) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("error", errorInfo);
        return jsonObject.toString();
    }
}
